<?php
namespace Home\Event;

use Org\Util\Sms;
use Org\Util\smtp;
class MessEvent 
{
    public function regverifyCode(){
        $type=I('type',1);
        $Verify = new \Think\Verify();
        $Verify->expire=120;//2分钟
        $Verify->fontSize=16;
        $Verify->entry($type);
    }
    
    /***
     * 短信验证码发送
     */
    public function sendMobileMes($mobile){
        if (!is_numeric($mobile)) {
            $this->ajaxReturn(array('code'=>'error', 'info' => '手机号码为空！'));
            exit;//0错误，1成功
        }
        else if(!preg_match('#^13[\d]{9}$|^14[5,7]{1}\d{8}$|^15[^4]{1}\d{8}$|^17[0,6,7,8]{1}\d{8}$|^18[\d]{9}$#', $mobile)) {
            $this->ajaxReturn(array('code'=>'error','info' => '手机号码格式错误！'));//0错误，1成功
            exit;
        }
        $code = mt_rand(100000, 999999);
        $text = '您的验证码是：' . $code . '。此验证码1分钟内有效，请不要把验证码泄露给其他人。如非本人操作，可不用理会！';
        $send_time = time();
        $expire_time  = time() + 60;
        $return = Sms::sendSms(array( 'content' => $text, 'mobile' => $mobile, 'expire_time'=>$expire_time,'extra'=>$code));
        if($return=='0' || $return==0) {
            session(array('name'=>'registerCode','expire'=>70));
            session('registerCode',$code);
            return array('code'=>'success','info' => '获取验证码成功，请及时查看！');//0错误，1成功
        } else {
            return array('code'=>'error','info' => '验证码发送失败！');//0错误，1成功
        }
    }
    /**
     * 邮箱验证码发送
     * */
    public function sendEmailMes($email){
        if(!preg_match('/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/', $email)) {
            return array('code'=>'error','info' => '邮箱地址格式错误！');//0错误，1成功
        }
        $code = mt_rand(100000, 999999);
        $mail_title = '欢迎您注册，您的验证码';
        $mail_body = <<<Eof
                            尊敬的 $email 用户您好，您的验证码是 ：$code 。请在1分钟之内使用！<br/>
                            http://www.chengjie.com <br/>
                            联系电话：+86 0513-886-99963<br/>
                            客服邮箱: admin@chengjie.com 
Eof;
        smtp::sendSmsContext($mail_body, $mail_title,$email);
        session(array('name'=>'registerCode','expire'=>70));
        session('registerCode',$code);
        return array('code'=>'success','info' => '邮件发送成功，请及时查看！');//0错误，1成功
    }
    
    /**
     * 校验验证码
     */
    public function checkCode($codePost){
        $result["status"]=1;
        $code = session('registerCode');//从session里获得的验证码对象
        $sescode=strval($code);
        if($codePost!=$sescode){
            $result["status"]=0;
            $result["msg"]='验证码错误或已过期！';
            return  $result;
        };
        return  $result;
    }
    
    function getIPaddress(){
        $IPaddress='';
        if (isset($_SERVER)){
            if (isset($_SERVER["HTTP_X_FORWARDED_FOR"])){
                $IPaddress = $_SERVER["HTTP_X_FORWARDED_FOR"];
            } else if (isset($_SERVER["HTTP_CLIENT_IP"])) {
                $IPaddress = $_SERVER["HTTP_CLIENT_IP"];
            } else {
                $IPaddress = $_SERVER["REMOTE_ADDR"];
            }
        } else {
            if (getenv("HTTP_X_FORWARDED_FOR")){
                $IPaddress = getenv("HTTP_X_FORWARDED_FOR");
            } else if (getenv("HTTP_CLIENT_IP")) {
                $IPaddress = getenv("HTTP_CLIENT_IP");
            } else {
                $IPaddress = getenv("REMOTE_ADDR");
            }
        };
        
        $taobaoIP = 'http://ip.taobao.com/service/getIpInfo.php?ip='.$IPaddress;
        $IPinfo = json_decode(file_get_contents($taobaoIP));
        $province = $IPinfo->data->region;
        $city = $IPinfo->data->city;
        $data = $province.$city;
        
        return array('ip'=> $IPaddress,"city"=> $data);
    }
}